import pdb
import json

class TDMessage:

    # param: str or dict variable
    def __init__(self, data):
        """main fields:
            id:
            from:
            to:
            body:
        """

        # Note: self._data is always type dict
        # TODO:
        # - check if the required fields are present

        if type(data) == str and type(json.loads(data)) == dict:
            self._data = json.loads(data)
        elif type(data) == dict:
            self._data = data 
        else:
            # TODO:
            # - have to raise an exception here
            pass

    # convert dict to json str
    def serialize(self):
        return json.dumps(self._data)

    # just return _data
    def deserialize(self):
        return self._data

def simple_test():
    x = TDMessage('{"id":"ABC1234", "from":"639000000000", "to":"639000000001", "body":"hello"}')
    y = TDMessage(x.deserialize()) 

    if y.serialize() == x.serialize():
        print 'OK'

if __name__ == "__main__":
    simple_test()
